#include "MainWindow.h"

MainWindow::MainWindow(QMainWindow *parent) : QMainWindow(parent){
	setupUi(this);
	fps = 60;
	//this->setCentralWidget(this->graphicsViewRight);

	this->m_producer = NULL;
	this->m_consumer = NULL;
	this->saveWindow = new SaveWindow();
	this->isPlaying = false;
	timer = new QTimer();
	timer->start(200);
	this->connectSlots();
}


MainWindow::~MainWindow(){
	delete m_consumer;
	delete m_producer;
	delete m_profile;
	Mlt::Factory::close();
}

void MainWindow::initializeMlt(){

	// Initialize the library and load plugins.
	Mlt::Factory::init (NULL);

	m_profile = new Mlt::Profile ("square_ntsc_wide");
	m_consumer = new Mlt::Consumer (*m_profile, "sdl_preview");
	m_playlist = NULL;
	m_tractor = NULL;

	// Embed the SDL window in our GUI.
	m_consumer->set("window_id", (int) this->videoWidget->winId());
	m_consumer->set("play.audio_buffer", 4096);
	QPalette pal;
	m_consumer->set("window_background", pal.color(QPalette::Window).name().toAscii().constData());

	// Load a color producer to clear the video region with black.
	m_producer = new Mlt::Producer(*m_profile, "color:");
	m_consumer->connect(*m_producer);
	m_producer->set_speed(0.0);
	m_consumer->start();
}

void MainWindow::connectSlots(){
	connect(this->actionOuvrir, SIGNAL(triggered()), this, SLOT(openVideo()));
	connect(this->actionLire, SIGNAL(triggered()), this, SLOT(playOrPause()));
	connect(this->actionArreter, SIGNAL(triggered()), this, SLOT(arreter()));
	connect(this->positionSlider, SIGNAL(sliderMoved(int)), this, SLOT(seek(int)));
	connect(this->buttonLire, SIGNAL(clicked()), this, SLOT(playOrPause()));
	connect(this->buttonArreter, SIGNAL(clicked()), this, SLOT(arreter()));
	//connect(this->buttonVoireListe, SIGNAL(clicked()), extractionManagerWindow, SLOT(show()));
	connect(timer, SIGNAL(timeout()), this, SLOT(updateSlider()));
	connect(buttonSauvegarder, SIGNAL(clicked()), this, SLOT(save()));
}

void MainWindow::openVideo(){
	QString fileName = QFileDialog::getOpenFileName(this);
	if(!fileName.isNull()){
		Mlt::Producer *producer = new Mlt::Producer(*m_profile, fileName.toUtf8().constData());
		//Mlt::Producer *producer = new Mlt::Producer(*m_profile, "color:red");
		if(producer->is_valid()){
			/*
			Mlt::Filter *filter = new Mlt::Filter(*m_profile, "dynamictext");
			Mlt::Properties *prop = new Mlt::Properties(filter->get_properties());
			//prop->set("argument", "some text");
			//prop->set("family", "Liberation Serif");
			prop->set("valign", "middle");
			producer->attach(*filter);
			m_playlist->append(*producer, 0);
			//*/

			/*
			//Mlt::Properties *prop = new Mlt::Properties(transition->get_properties());
			//prop->set("in", 0);
			//prop->set("out", 400);
			//prop->set("start", "0,0:100%x100%");
			//prop->set("factory", "loader");
			//prop->set("aligned", "1");
			//prop->set("deinterlace", 0);
			//prop->set("geometry", "0%,0%:100%x100%:100");
			//prop->set("operator", "over");
			//prop->set("softness", 0);
			//prop->set("luma_invert", 0);
			//prop->set("progressive", 1);
			//prop->set("geometry", "0=0%,0%:100%x100%:100; -1=0%,0%:100%x100%:100;");
			//prop->set("geometry", "0=0%/0%:100%x100%:50;-1=0%/0%:100%x100%:100");
			//prop->set("halign", "centre");
			//prop->set("valign", "centre");
			//prop->set("geometry", "0=0%/0%:100%x100%:100");
			//prop->set("geometry", "0=0%/0%:100%x100%:50");
			//prop->set("distort", 1);
			//prop->set("fill", 1);
			Mlt::Producer *producer = new Mlt::Producer(*m_profile, fileName.toUtf8().constData());
			Mlt::Properties *propProd = new Mlt::Properties(producer->get_properties());
			propProd->set("out", 400);
			Mlt::Producer *subTitleProducer = new Mlt::Producer(*m_profile, "cedric_01.svg");
			Mlt::Properties *propSub = new Mlt::Properties(subTitleProducer->get_properties());
			propSub->set("out", 400);
			//propSub->set("a_track", 1);
			//propSub->set("b_track", 0);
			Mlt::Transition *transition = Mlt::Factory::transition(*m_profile, "composite", NULL);
			Mlt::Properties *prop = new Mlt::Properties(transition->get_properties());
			//prop->set("geometry", "0=0%/0%:100%x100%:50");
			//prop->set("distort", 1);
			//prop->set("fill", 1);
			prop->set("out", 400);
			m_playlist = new Mlt::Playlist();
			//m_playlist->append(*producer, 0);
			//m_playlist->append(*subTitleProducer, 0, 400);
			m_playlist->insert(*subTitleProducer, 0, 0, 400);
			m_playlist->insert(*producer, 1);
			m_playlist->mix(0, 400, transition);
			//std::cout << "a_track :" << transition->get_a_track() << std::endl;
			//std::cout << "b_track :" << transition->get_b_track() << std::endl;
			//transition->connect(*m_playlist, 0, 1);
			//*/
			Mlt::Tractor *tractor = new Mlt::Tractor();
			Mlt::Multitrack *multitrack = tractor->multitrack();
			Mlt::Producer *colorProducer = new Mlt::Producer(*m_profile, "color");
			Mlt::Properties *propColor = new Mlt::Properties(producer->get_properties());
			propColor->set("in", 0);
			propColor->set("out", 400);
			propColor->set("novdpau", 1);
			multitrack->connect(*colorProducer, 0);
			Mlt::Producer *videoProducer = new Mlt::Producer(*m_profile, fileName.toUtf8().constData());
			Mlt::Properties *propVideo = new Mlt::Properties(producer->get_properties());
			multitrack->connect(*videoProducer, 1);
			propVideo->set("out", 400);
			propVideo->set("novdpau", 1);
			Mlt::Producer *subTitleProducer = new Mlt::Producer(*m_profile, "cedric_01.svg");
			multitrack->connect(*subTitleProducer, 2);
			Mlt::Properties *propSub = new Mlt::Properties(subTitleProducer->get_properties());
			propSub->set("out", 400);
			Mlt::Transition *transition1 = Mlt::Factory::transition(*m_profile, "composite", NULL);
			Mlt::Properties *propTrans01 = new Mlt::Properties(transition1->get_properties());
			propTrans01->set("out", 400);
			propTrans01->set("progressive", 1);
			//propTrans01->set("geometry", "0=0%,0%:100%x100%:100;-1=0%,0%:100%x100%:100;");
			propTrans01->set("start", "0,0:100%x100%");
			propTrans01->set("geometry", "0%,0%:100%x100%:100");
			//propTrans01->set("halign", "centre");
			//propTrans01->set("valign", "centre");
			propTrans01->set("distort", 1);
			propTrans01->set("fill", 1);
			tractor->plant_transition(transition1, 0, 1);
			Mlt::Transition *transition2 = Mlt::Factory::transition(*m_profile, "mix", NULL);
			Mlt::Properties *propTrans02 = new Mlt::Properties(transition2->get_properties());
			propTrans02->set("out", 400);
			propTrans02->set("combine", 1);
			propTrans02->set("always_active", 1);
			tractor->plant_transition(transition2, 0, 1);
			Mlt::Transition *transition3 = Mlt::Factory::transition(*m_profile, "composite", NULL);
			Mlt::Properties *propTrans03 = new Mlt::Properties(transition3->get_properties());
			propTrans03->set("out", 400);
			propTrans03->set("progressive", 1);
			propTrans03->set("start", "0,0:100%x100%");
			propTrans03->set("geometry", "0%,0%:100%x100%:100");
			//propTrans03->set("geometry", "0=0%,0%:100%x100%:100;-1=0%,0%:100%x100%:100;");
			//propTrans03->set("halign", "centre");
			//propTrans03->set("valign", "centre");
			//propTrans03->set("distort", 0);
			propTrans03->set("fill", 1);
			tractor->plant_transition(transition3, 0, 2);
			m_tractor = tractor;

			//Mlt::Properties *propTransition = new Mlt::Properties(transition->get_properties());
			//propTransition->set("out", 400);
			//tractor->plant_transition(transition, 1, 0);
			m_consumer->stop();
			m_consumer->connect(*tractor);
			delete m_producer;
			m_producer = producer;
			m_consumer->start();
			play();
		}
		//fps = m_producer->get_fps();
	}
	// If file invalid, then on some platforms the dialog messes up SDL.
	m_consumer->set ("refresh", 1);
}

void MainWindow::play(){
	if(m_playlist != NULL){
		m_playlist->set_speed(1.0);
	}else if(m_tractor != NULL){
		std::cout << "before" << std::endl;
		m_tractor->set_speed(1.0);
		std::cout << "after" << std::endl;
	}
	// If we are paused, then we need to "unlock" sdl_still.
	if(m_consumer->is_stopped()){
		m_consumer->start();
	}
	m_consumer->set("refresh", 1);
	//this->statusBar->showMessage(tr("Playing"));
	isPlaying = true;
	this->buttonLire->setText("Pause");
}

void MainWindow::pause(){
	m_playlist->set_speed(0.0);
	isPlaying = false;
	this->buttonLire->setText("Lire");
}

void MainWindow::playOrPause(){
	if(isPlaying){
		//TODO il faudrait aussi changer les icons
		this->pause();
	}else{
		this->play();
	}
}

void MainWindow::seek(int position){
	int producerPosition = m_producer->get_length() / this->positionSlider->maximum() * position;
	//m_producer->seek(producerPosition);
	m_playlist->seek(producerPosition);
}

void MainWindow::arreter(){
	m_playlist->set_speed(0.0);
	m_playlist->seek(0);
	positionSlider->setValue(0);
	isPlaying = false;
}

void MainWindow::updateSlider(){
	if(!this->positionSlider->isSliderDown()){
		int sliderValue = 1.0 * ((int)m_producer->position()) / m_producer->get_length() * this->positionSlider->maximum();
		this->positionSlider->setValue(sliderValue);
	}
}

void MainWindow::save(){
	saveWindow->setPlaylist(m_playlist);
	//arreter();
	m_playlist->set_speed(0.0);
	m_playlist->seek(0);
	m_playlist->pause();
	m_consumer->stop();
	saveWindow->show();
}

